﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using WaterRevenueMS.Componnent.Entities.ReadMeterMS;
using WaterRevenueMS.Core.Repository.ReadMeterMS;
using WaterRevenueMS.Componnent.Common;

namespace WaterRevenueMS.Core.Service.ReadMeterMS.Impl
{
    [Export(typeof(IMeterRecordService))]
    public class MeterRecordService:CoreServiceBase,IMeterRecordService
    {
        [Import]
        private IMeterRecordRepository MeterRecordRepository { get; set; }

        [Import]
        public WaterRevenueMS.Core.Repository.Customer.IMeterInfoRepository MeterInfoRepository { get; set; }

        public IQueryable<MeterRecord> MeterRecords
        {
            get { return MeterRecordRepository.Entities; }
        }



        public OperationResult Insert(MeterRecord meterRecord,bool isSave)
        {
            MeterRecordRepository.Insert(meterRecord, isSave);
            return new OperationResult(OperationResultType.Success, "成功");
        }

        public OperationResult Update()
        {
            throw new NotImplementedException();
        }

        public OperationResult Delete(int Id)
        {
            throw new NotImplementedException();
        }
        public int InsertDTFromExl(Domain.ViewModels.ReadMeterMS.MeterRecord.MeterRecordModel model)
        {

            var entity = MeterInfoRepository.Entities.FirstOrDefault(t => t.Yi_BBH == model.Yi_BBH);
            
            if (entity.RMeter_No != null)
            {
                entity.Chao_BDTBC = model.Chao_BDT;
                entity.Du_SBC = model.Du_SBC;
                var entityw = MeterRecords.FirstOrDefault(t => t.Meter_No == entity.Id && t.Ren_WMBBH == entity.RMeter_No);
                if (entityw != null)
                {
                    entityw.Du_SBC = model.Du_SBC;
                    entityw.Chao_BDT = model.Chao_BDT;
                    entityw.Yi_BZT = model.Yi_BZT;

                    MeterRecordRepository.Update(entityw);

                }
                else
                {
                    return 2;
                }

                MeterInfoRepository.Update(entity);
                return 1;
            }
            else
            {
                return 0;
            }
               
        }
        
       
    }
}
